(function (vc, vm) {

    vc.extends({
        data: {
            editAffiliateRuleInfo: {
                ruleId: '',
                name: '',
                level1Rate: '',
                level2Rate: '',
                level21Rate: '',
                expireDay: '',
            }
        },
        _initMethod: function () {

        },
        _initEvent: function () {
            vc.on('editAffiliateRule', 'openEditAffiliateRuleModal', function (_params) {
                vc.component.refreshEditAffiliateRuleInfo();
                $('#editAffiliateRuleModel').modal('show');
                vc.copyObject(_params, vc.component.editAffiliateRuleInfo);
                vc.component.editAffiliateRuleInfo.communityId = vc.getCurrentCommunity().communityId;
            });
        },
        methods: {
            editAffiliateRuleValidate: function () {
                return vc.validate.validate({
                    editAffiliateRuleInfo: vc.component.editAffiliateRuleInfo
                }, {
                    'editAffiliateRuleInfo.name': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "规则名称不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "30",
                            errInfo: "规则名称不能超过30"
                        },
                    ],
                    'editAffiliateRuleInfo.level1Rate': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "一级分销佣金比例不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "32",
                            errInfo: "一级分销佣金比例不能为空"
                        },
                    ],
                    'editAffiliateRuleInfo.expireDay': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "过期时间不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "200",
                            errInfo: "过期时间不能为空"
                        },
                    ],
                    'editAffiliateRuleInfo.ruleId': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "规则ID不能为空"
                        }]

                });
            },
            editAffiliateRule: function () {
                if (!vc.component.editAffiliateRuleValidate()) {
                    vc.toast(vc.validate.errInfo);
                    return;
                }

                vc.http.apiPost(
                    'affiliateRule.updateAffiliateRule',
                    JSON.stringify(vc.component.editAffiliateRuleInfo),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            $('#editAffiliateRuleModel').modal('hide');
                            vc.emit('affiliateRuleManage', 'listAffiliateRule', {});
                            return;
                        }
                        vc.message(_json.msg);
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');

                        vc.message(errInfo);
                    });
            },
            refreshEditAffiliateRuleInfo: function () {
                vc.component.editAffiliateRuleInfo = {
                    ruleId: '',
                    name: '',
                    level1Rate: '',
                    level2Rate: '',
                    level21Rate: '',
                    expireDay: '',
                }
            }
        }
    });

})(window.vc, window.vc.component);
